package com.derschmale.wick3d.core.rasterizers
{
	import com.derschmale.wick3d.core.data.GeometryData;
	import com.derschmale.wick3d.core.data.RenderPipelineData;
	import com.derschmale.wick3d.core.geometry.Triangle3D;
	import com.derschmale.wick3d.debug.GeneralStatData;
	import com.derschmale.wick3d.view.Viewport;
	
	public class PainterRasterizer
	{
		public function rasterize(viewport : Viewport, pipelineData : RenderPipelineData, data : GeometryData = null) : void
		{
			var triangle : Triangle3D; 
			var i : int;
			var tris : Array;
			
			if (data) {
				tris = data.triangles;
			}
			else {
				tris = pipelineData.visibleTriangles;;
			}
			tris.sortOn("zIndex", Array.NUMERIC);
			
			i = tris.length;
			
			while (triangle = tris[--i] as Triangle3D) {
				if (!triangle.isCulled) {
					triangle.material.drawTriangle(triangle, viewport, pipelineData);
					GeneralStatData.drawnPolygons++;
				}
			}
		}
	}
}